#region Copyright
/// <copyright>
/// Copyright (c) 2007 Ilia Shramko.
/// Released under the terms of the GNU General Public License.
/// </copyright>
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Osh.Teletext
{
	public class TeletextTextWriter : TeletextWriter
	{
		public class TeletextTextWriterSettings
		{
			private bool _endRowWithNewLine = true;

			public bool EndRowWithNewLine
			{
				get { return _endRowWithNewLine; }
				set { _endRowWithNewLine = value; }
			}
		}

		private TextWriter _writer;
		private TeletextTextWriterSettings _settings;
		private bool _firstLineWritten;
		private bool _newLineWritten;

		public TeletextTextWriter(TextWriter writer)
		{
			_writer = writer;
			_settings = new TeletextTextWriterSettings();
		}

		public override void WriteStartRow(int row)
		{
			if(_firstLineWritten && !_newLineWritten)
				_writer.WriteLine();

			_newLineWritten = false;
		}

		public override void WriteEndRow()
		{
			if(_settings.EndRowWithNewLine)
			{
				_writer.WriteLine();
				_newLineWritten = true;
			}
			_firstLineWritten = true;
		}

		public override void WriteChars(char[] buffer, int offset, int count)
		{
			_writer.Write(buffer, offset, count);
		}

		public override void WriteChar(char ch)
		{
			_writer.Write(ch);
		}

		public override void Flush()
		{
			_writer.Flush();
		}

		public override void Close()
		{
			_writer.Close();
		}

		public TeletextTextWriterSettings Settings
		{
			get { return _settings; }
		}
	}
}
